var lastIndex=-1;
var url;
$(function(){
	 $('#addOperatorDialog').dialog({
		 	modal:true,
		 	closed:true,
			toolbar:[{
				text:'保存',
				iconCls:'icon-save',
				handler:function(){
	                $('#addOperatorForm').form('submit',{
	    				url: url,
	    				onSubmit: function(){
	    					 if($(this).form('validate')){
	    					 	return true;
	    					 }else{
	    					 	$.messager.alert('提示：','请填写完整信息','error');
	    					 	return false;
	    					 }
	    				},
	    				success: function(data){
	    					var result = eval('('+data+')');
	    					if (result.success){
	    						$('#addOperatorDialog').dialog('close');
	    						$('#operatorTable').datagrid('reload');
	    					} else {
    							$.messager.alert('提示：','登陆名重复','error');
	    					}
	    				}
	    			});
				}
			 },'-',{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#addOperatorDialog').dialog('close');
				}
			}]
		});
	
	$("#add").click(function(){
		$(".password").show();
		$('#addOperatorDialog').dialog('open').dialog('setTitle','新增用户');
		$('#addOperatorDialog').form('clear');
		$('#password').val(123456);
		url = js+'system/addOperator.action'
		return false;
	});
	
	
	/*删除用户*/
	$("#detele").click(function(){
		var idsArray = new Array();
		var rows = $('#operatorTable').datagrid('getSelections');
		if(rows.length==0){
			$.messager.alert("提示","请选中数据","warning");
			return false;
		}
		$.messager.confirm("提示！","确定要删除选中用户?",function(t){ 
			if(!t) return;
			for(var i=0;i<rows.length;i++){
				idsArray.push(rows[i].operatorID_);
			}	
			var ids= idsArray.join(",");
			$.post(js+"system/deleteByIDsOperator.action",{ids:ids},function(result){
			     if(result.success){
					 $('#operatorTable').datagrid('reload');
			      }else{
			        $.messager.alert('提示',"删除不成功","error");	
			      }
			},"json");
			
		});
		
		return false;
	});
	
	$('#operatorTable').datagrid({
				fit:true,
				url:js+'system/viewOperator.action',
				idField:'operatorID_',
				height: 'auto',
				columns:[[{field:'operatorID_',checkbox:true},
	                {field:'operatorCode',title:'登陆名称',width:80,sortable:true,editor:'text'},
					{field:'operatorName',title:'姓名',width:120,sortable:true,editor:'text'}
				]],
				onClickRow:function(rowIndex){
					lastIndex = rowIndex;
				},onDblClickRow:function(rowIndex, rowData){
				    $(".password").hide()
					$('#addOperatorDialog').dialog('open').dialog('setTitle','修改用户');
					$('#addOperatorDialog').form('load',rowData);
					url = js+'system/updateOperator.action'
				},
				pagination:true,
				rownumbers:true
	});
	
	
})

